package com.gpnu7_412.yanzhi.service.impl;

import com.gpnu7_412.yanzhi.dto.CommentDisplayDto;
import com.gpnu7_412.yanzhi.entity.Comment;
import com.gpnu7_412.yanzhi.entity.CommentHanding;
import com.gpnu7_412.yanzhi.entity.User;
import com.gpnu7_412.yanzhi.exception.custom.DataNotFoundException;
import com.gpnu7_412.yanzhi.exception.custom.PermissionDeniedException;
import com.gpnu7_412.yanzhi.exception.custom.SQLExecuteException;
import com.gpnu7_412.yanzhi.mapper.CommentHandingMapper;
import com.gpnu7_412.yanzhi.mapper.CommentMapper;
import com.gpnu7_412.yanzhi.mapper.DynamicMapper;
import com.gpnu7_412.yanzhi.mapper.UserMapper;
import com.gpnu7_412.yanzhi.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author ZZH
 */
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CommentHandingMapper commentHandingMapper;
    @Autowired
    private DynamicMapper dynamicMapper;

    /**
     * 添加评论
     * @param comment 评论信息
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addComment(Comment comment) {
        if (comment == null) {
            return false;
        }
        if (dynamicMapper.getDynamicById(comment.getDynamicId()) == null) {
            throw new DataNotFoundException("评论的动态不存在");
        }
        if (commentMapper.insertComment(comment) != 1) {
            throw new SQLExecuteException("评论失败，请重试或联系管理员");
        }
        return true;
    }

    /**
     * 删除评论
     * @param commentId 待删评论ID
     * @param userId 操作者的用户ID
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteComment(Integer commentId, Integer userId) {
        if (commentId == null || userId == null) {
            return false;
        }
        User user = userMapper.selectById(userId);
        if (user == null) {
            throw new DataNotFoundException("操作用户不存在");
        }
        Comment comment = commentMapper.getCommentByCommentId(commentId);
        if (comment == null) {
            throw new DataNotFoundException("待删评论不存在");
        }
        if ("管理员".equals(user.getPermission()) || comment.getUserId().equals(userId)) {
            if (commentMapper.deleteByCommentId(commentId) != 1) {
                throw new SQLExecuteException("评论删除失败，请重试或联系管理员");
            }
            return true;
        }
        throw new PermissionDeniedException("权限不足");
    }

    /**
     * 修改评论
     * @param comment 评论对象
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateComment(Comment comment, Integer userId) {
        if (comment == null || userId == null) {
            return false;
        }
        User user = userMapper.selectById(userId);
        if (user == null) {
            throw new DataNotFoundException("操作用户不存在");
        }
        Comment selectComment = commentMapper.getCommentByCommentId(comment.getCommentId());
        if (selectComment == null) {
            throw new DataNotFoundException("待修改的评论不存在");
        }
        if ("管理员".equals(user.getPermission()) || selectComment.getUserId().equals(userId)) {
            if (commentMapper.updateComment(comment) != 1) {
                throw new SQLExecuteException("评论修改失败，请重试或联系管理员");
            }
        } else {
            throw new PermissionDeniedException("权限不足");
        }
        return true;
    }

    /**
     * 点赞评论
     * @param commentId 评论ID
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean likeComment(Integer commentId) {
        if (commentId == null) {
            return false;
        }
        if (commentMapper.likeComment(commentId) != 1) {
            throw new SQLExecuteException("评论点赞失败，请重试或联系管理员");
        }
        return true;
    }

    /**
     * 举报评论
     * @param commentHanding 评论举报对象
     * @return boolean
     */
    @Override
    public boolean reportComment(CommentHanding commentHanding) {
        if (commentHanding == null) {
            return false;
        }
        if (commentMapper.getCommentByCommentId(commentHanding.getCommentId()) == null) {
            throw new DataNotFoundException("举报的评论不存在");
        }
        if (commentHandingMapper.insert(commentHanding) != 1) {
            throw new SQLExecuteException("评论举报失败，请重试或联系管理员");
        }
        return true;
    }

    // 根据评论ID获取评论
    @Override
    public Comment getByCommentId(Integer commentId) {
        if (commentId == null) {
            return null;
        }
        Comment comment = commentMapper.getCommentByCommentId(commentId);
        if (comment == null) {
            throw new DataNotFoundException("查无此评论");
        }
        return comment;
    }

    /**
     * 根据动态ID获取评论展示DTO
     * @param dynamicId 动态ID
     * @return 评论展示列表
     */
    @Override
    public List<CommentDisplayDto> getByDynamicId(Integer dynamicId) {
        if (dynamicId == null) {
            return null;
        }
        return commentMapper.selectCommentDtoByDynamicId(dynamicId);
    }
}
